package com.sname.car.sms;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sname.car.common.HttpUtil;
import com.sname.car.common.constants.Constants;
import com.sname.car.security.MD5Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
/**
 * Created By  醉美柳舞之众星捧月
 * @author songx
 * @date 2018/9/25 11:52
 */
@Service("verCodeService")
public class MiaoDiSmsSender implements SmsSender {
    private Logger log = LoggerFactory.getLogger(getClass());
    private static String operation = "/industrySMS/sendSMS";
    @Override
    public void sendSms(String phone, String tplId, String params) {
        try {
            String pattern = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            String date = sdf.format(new Date());
            String sig = MD5Utils.getMd5(Constants.MDSMS_ACCOUNT_SID + "." + Constants.MDSMS_AUTH_TOKEN + "." + date);
            String url = Constants.MDSMS_REST_URL + operation;
            HashMap<String, String> map = new HashMap<>();
            map.put("accountSid", Constants.MDSMS_ACCOUNT_SID);
            map.put("to", phone);
            map.put("templateid", tplId);
            map.put("map", params);
            map.put("date", date);
            map.put("sig", sig);
            map.put("respDataType", "json");
            String result = HttpUtil.post(url, map);
            JSONObject jsonObject = JSON.parseObject(result);
            if (jsonObject.getString("respCode").equals("00000")) {
                log.error("fail to send sms to " + phone + ":" + params + ":" + result);
            }
        } catch (Exception e) {
            log.error("fail to send sms to " + phone + ":" + params);
        }
    }
}
